const baseUrl = `${window.location.origin}/`;
//判断新增还是编辑：0为新增，1为编辑
var editIndex = 0;
layui.use(['element','form','table','layer','laydate'], function () {
    var table = layui.table;
    var layer = layui.layer;
    var form = layui.form;
    var laydate = layui.laydate;
    $ = layui.jquery;

    //执行一个laydate实例
    laydate.render({
        elem: '#beginDate' //指定元素
        ,type: 'datetime'
    });
    laydate.render({
        elem: '#endDate' //指定元素
        ,type: 'datetime'
    });

    table.render({
        elem: '#itemTable'
        ,cellMinWidth: 80
        ,method: 'post'
        ,url: baseUrl+'/eleinspect/item/getItemTable'
        ,title: '点位管理表'
        ,toolbar: '#toolbarDemo' //开启头部工具栏
        ,page: true
        ,cols: [[
            {type: 'checkbox', fixed: 'left'}
            ,{type:'numbers',title:'序号'}
            ,{field:'itemsName', title:'项目名称', align:'center'}
            ,{field:'itemsDetail', title:'项目描述', align:'center'}
            ,{field:'itemsCreateTime', title:'创建时间', align:'center'}
            ,{field:'right', title:'操作', align:'center',templet:
                    function (data) {
                        return '<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a><a class="layui-btn  layui-btn-normal layui-btn-xs" lay-event="detail">查看</a><a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete">删除</a>'
                    }}
        ]]
    });

    /*table.on('checkbox(pointTable)',function (obj) {
        console.log(obj.checked); //当前是否选中状态
        console.log(obj.data); //选中行的相关数据
        console.log(obj.type); //如果触发的是全选，则为：all，如果触发的是单选，则为：one
    });*/

    table.on('tool(itemTable)',function (obj) {
        var rowData = obj.data;
        if (obj.event === 'edit') {
            editIndex = 1;
            $("#itemsId").val(rowData.itemsId);
            $("#itemsName").val(rowData.itemsName);
            $("#itemsDetail").val(rowData.itemsDetail);
            $("#controller").show();
            layer.open({
                type: 1,
                title:"编辑",
                offset: 'auto',
                skin:'demo-class',
                closeBtn: 1,
                shift: 2,
                area: ['500px', '500px'],
                shadeClose: true,
                content: $("#item_main"),
                skin: 'to-fix-select'
            });
        }else if(obj.event === 'detail'){
            //表单填充
            $("#itemsId").val(rowData.itemsId);
            $("#itemsName").val(rowData.itemsName);
            $("#itemsDetail").val(rowData.itemsDetail);
            $("#controller").hide();
            layer.open({
                type: 1,
                title:"查看",
                offset: 'auto',
                skin:'demo-class',
                closeBtn: 1,
                shift: 2,
                area: ['500px', '500px'],
                shadeClose: true,
                content: $("#item_main"),
                skin: 'to-fix-select'
            });
        }else if(obj.event === 'delete'){
            layer.confirm('确定删除?',{btn:['确定','返回']},function (index) {
                $.ajax({
                    url:baseUrl+"/eleinspect/item/deleteItem",
                    type:'post',
                    data:{
                        itemsId: rowData.itemsId
                    },
                    success:function(data){
                        if (data.code == 0) {
                            layer.msg(data.msg,{icon:1});
                        }else{
                            layer.alert(data.msg,{icon:2});
                        }
                        active['reload'].call(this);
                    }
                });
            });
        }
    });

    $('#toAdd').on('click',function () {
        //初始化
        editIndex = 0;
        $("#itemsId").val("");
        $("#itemsName").val("");
        $("#itemsDetail").val("");
        form.render();
        layer.open({
            type: 1,
            title:"新增",
            offset: 'auto',
            closeBtn: 1,
            shift: 2,
            area: ['500px', '500px'],
            shadeClose: true,
            content: $("#item_main"),
            skin: 'to-fix-select',
        });
    });

    form.on("submit(addBtn)",function (data) {
        layer.confirm('是否确认提交？',{btn: ['确定','返回']},function (index) {
            $.ajax({
                url: baseUrl+"/eleinspect/item/saveItemInfo",
                type: 'post',
                data: {
                    itemsId: $("#itemsId").val(),
                    itemsName: $("#itemsName").val(),
                    itemsDetail: $("#itemsDetail").val(),
                    editIndex: editIndex
                }
                ,success:function(data){
                    layer.closeAll();
                    if (data.code == 0) {
                        layer.alert(data.msg,{icon:1});
                    }else{
                        layer.alert(data.msg,{icon:2});
                    }
                    location.reload();
                }
            });
        });
        return false;
    });

    form.on('submit(search)',function (data) {
        var searchText = $('#searchText').val();
        table.reload('itemTable', {
            page: {
                curr: 1
            },
            where: {
                itemsName: searchText.trim(),
                beginDate: $("#beginDate").val(),
                endDate: $("#endDate").val()
            }
        }, 'data');
        return false;
    });

    active = {
        reload: function(){
            //执行重载
            table.reload('itemTable', {

            });
        }
    }

});

function reload(){
    $("#searchText").val("");//火狐需要
    $("#beginDate").val("");
    $("#endDate").val("");
    location.reload();
}